package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.MerchantOrderItemDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by marco on 2017/6/28.
 */
@Repository
public class CustomerShareRepositoryImpl implements CustomerShareRepositoryCustom {

    private Logger log = LoggerFactory.getLogger(CustomerShareRepositoryImpl.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    /**
     * 用户分享产品产生的订单数据
     * @param page
     * @param customerId
     * @param productId
     * @return
     */
    @Override
    public PageResult<MerchantOrderItemDto> findMerchaOrderItemByShareIdAndProductId(PageResult page, Long customerId, Long productId) {
        SQLPageDoing<MerchantOrderItemDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from merchant_order_item moi where 1=1");
            sql.append(" and moi.sharer_id is not null");
            sql.append(" and moi.customer_id is not null");
            sql.append(" and moi.gift_packet_id is null");
            sql.append(" and moi.customer_id != :customerId");

            sql.append(" and moi.sharer_id = :customerId");
            params.put("customerId", customerId);

            sql.append(" and moi.product_id = :productId");
            params.put("productId", productId);

            fields.append("moi.customer_name sharerNickName, moi.product_name productName, moi.single_amount baseAmount,");
            fields.append("moi.settle_status settleStatus, moi.date_settled dateSettled, moi.date_created dateCreated,");
            fields.append("moi.logo_url logoUrl,");
            fields.append("(moi.share_fee*moi.share_rate/100) shareFee");
        };
        return doing.go(page, MerchantOrderItemDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     * 用户分享礼包产生的订单数据
     * @param page
     * @param customerId
     * @param giftPacketId
     * @return
     */
    @Override
    public PageResult<MerchantOrderItemDto> findMallOrderByShareIdAndGiftPacketId(PageResult page, Long customerId, Long giftPacketId) {
        SQLPageDoing<MerchantOrderItemDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_order mo,");
            sql.append(" (select DISTINCT mall_order_num,sharer_id,customer_name,share_rate,gift_packet_id from merchant_order_item");
            sql.append(" where gift_packet_id is not null and sharer_id is not null and gift_packet_id=:giftPacketId and sharer_id=:customerId) moi");
            sql.append(" where mo.order_num = moi.mall_order_num");
            sql.append(" and mo.gift_id = moi.gift_packet_id");
            sql.append(" and mo.customer_id != :customerId");

            params.put("customerId", customerId);

            sql.append(" and mo.gift_id = :giftPacketId");
            params.put("giftPacketId", giftPacketId);

            fields.append("moi.customer_name sharerNickName, mo.gift_name giftName, (select gp.sell_price from gift_packet gp where gp.id=mo.gift_id) baseAmount,");
            fields.append("mo.status settleStatus, mo.last_updated dateSettled, mo.date_created dateCreated,");
            fields.append("(select gp.cover_url from gift_packet gp where gp.id=mo.gift_id) logoUrl,");
            fields.append("mo.share_fee shareFee");
        };
        return doing.go(page, MerchantOrderItemDto.class, objectMapper, jdbcTemplate, log);
    }
}
